package com.edu.whty.controller;

import com.edu.whty.common.constants.CodeConstants;
import com.edu.whty.common.tools.UUIDUtils;
import com.edu.whty.entity.base.ResponseEntity;
import com.edu.whty.entity.base.SpecialRepo;
import com.edu.whty.entity.school.SpecialResourcesBase;
import com.edu.whty.service.SpecialRepoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * ClassName: SpecialRepoController Description: TODO 专题目录设置
 * date: 2023/5/9 14:35
 *
 * @author khye
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/specialRepo")
public class SpecialRepoController {

    @Autowired
    private SpecialRepoService specialRepoService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * Description:TODO 批量新增目录
     * @author:khye
     * @date: 2023/5/9 15:00
     * @param:[specialRepos]
     * @return:com.edu.whty.entity.base.ResponseEntity
     */
    @RequestMapping(value = "/addSpecialRepos", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity addSpecialRepos(@RequestBody List<SpecialRepo> specialRepos){
        ResponseEntity responseEntity = ResponseEntity.getSuccessResponse();
        for (SpecialRepo specialRepo : specialRepos){
            specialRepo.setId(UUIDUtils.getRandomUUID());
            specialRepo.setCreateTime(sdf.format(new Date()));
            specialRepo.setStatus("0");
        }
        specialRepoService.batchAddSpecialRepo(specialRepos);
        return responseEntity;
    }

    /**
     * Description:TODO 批量编辑目录
     * @author:khye
     * @date: 2023/5/9 15:23
     * @param:[specialRepos]
     * @return:com.edu.whty.entity.base.ResponseEntity
     */
    @RequestMapping(value = "/editSpecialRepos", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity editSpecialRepos(@RequestBody List<SpecialRepo> specialRepos){
        ResponseEntity responseEntity = ResponseEntity.getSuccessResponse();
        if (CollectionUtils.isEmpty(specialRepos)){
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "目录不能为空");
        }
        Map<String,List<SpecialRepo>> specialRepoMap = specialRepos.stream().collect(Collectors.groupingBy(SpecialRepo::getDealSign));
        if (!CollectionUtils.isEmpty(specialRepoMap.get("1"))){
            specialRepoService.batchUploadSpecialRepo(specialRepoMap.get("1"));
        }
        if (!CollectionUtils.isEmpty(specialRepoMap.get("0"))){
            for (SpecialRepo specialRepo : specialRepoMap.get("0")){
                specialRepo.setId(UUIDUtils.getRandomUUID());
                specialRepo.setCreateTime(sdf.format(new Date()));
                specialRepo.setStatus("0");
            }
            specialRepoService.batchAddSpecialRepo(specialRepoMap.get("0"));
        }
        if (!CollectionUtils.isEmpty(specialRepoMap.get("-1"))){
            specialRepoService.batchDeleteSpecialRepos(specialRepoMap.get("-1"));
        }
        return responseEntity;
    }

    /**
     * Description:TODO 修改教师目录
     * @author:khye
     * @date: 2023/5/11 10:00
     * @param:[specialResourcesBases]
     * @return:com.edu.whty.entity.base.ResponseEntity
     */
    @RequestMapping(value = "/editUserCatalog", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity editUserCatalog(@RequestBody List<SpecialResourcesBase> specialResourcesBases){
        ResponseEntity responseEntity = ResponseEntity.getSuccessResponse();
        for (SpecialResourcesBase specialResourcesBase : specialResourcesBases){
            if (StringUtils.isEmpty(specialResourcesBase.getId())){
                return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "id不能为空");
            }
        }
        specialRepoService.editUserCatalog(specialResourcesBases);
        return responseEntity;
    }

}
